home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 79 / maccd 79.iso / multimedial / GL Tron / Source / gltron / sound_sdl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-20  |  2.6 KB  |  146 lines  |  [TEXT/CWIE]

  1. #include "sound.h"
  2. #include "gltron.h"
  3.  
  4. /* linux only, at the moment */
  5.  
  6. static Mix_Music *music;
  7. static int sound_open = 0;
  8. #define SOUND_IS_OK if(!sound_open) return;
  9. /*
  10. #define NUM_GAME_FX 5
  11. #define NUM_MENU_FX 2
  12. */
  13. #define NUM_GAME_FX 2
  14. #define NUM_MENU_FX 2
  15.  
  16. Mix_Chunk *game_fx[NUM_GAME_FX];
  17. static Mix_Chunk *menu_fx[NUM_MENU_FX];
  18.  
  19. static char *game_fx_names[] = {
  20.   "game_engine.wav",
  21.   "game_crash.wav",
  22.  
  23.   "game_start.wav",
  24.   "game_win.wav",
  25.   "game_lose.wav"
  26. };
  27.  
  28. static char *menu_fx_names[] = {
  29.   "menu_action.wav",
  30.   "menu_highlight.wav"
  31. };
  32.  
  33. void loadFX() {
  34.   int i;
  35.   char *path;
  36.  
  37.   SOUND_IS_OK;
  38.  
  39.   for(i = 0; i < NUM_GAME_FX; i++) {
  40.     path = getFullPath(game_fx_names[i]);
  41.     if(path) {
  42.       game_fx[i] = Mix_LoadWAV(path);
  43.       free(path);
  44.     } else {
  45.       printf("error: can't load sound file %s\n",
  46.          game_fx_names[i]);
  47.       exit(1);
  48.     }
  49.   }
  50.  
  51.   for(i = 0; i < NUM_MENU_FX; i++) {
  52.     path = getFullPath(menu_fx_names[i]);
  53.     if(path) {
  54.       menu_fx[i] = Mix_LoadWAV(path);
  55.       free(path);
  56.     }
  57.   }
  58. }
  59.  
  60. int initSound() {
  61.   /* open the audio device */
  62.   if(Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 1024) < 0) {
  63.     fprintf(stderr, "can't open audio: %s\n", SDL_GetError());
  64.     return 1;
  65.   }
  66.   sound_open = 1;
  67.  
  68.   loadFX();
  69.   return 0;
  70. }
  71.  
  72.  
  73. void shutdownSound() {
  74.   SOUND_IS_OK;
  75.   Mix_CloseAudio();
  76. }
  77.   
  78.  
  79. void loadSound(char *name) {
  80.   SOUND_IS_OK;
  81.   music = Mix_LoadMUS(name);
  82.   return;
  83. }
  84.  
  85. void playSound() {
  86.   SOUND_IS_OK;
  87.   if( ! Mix_PlayingMusic() )
  88.     Mix_PlayMusic(music, -1);
  89.   /* todo: remove the following once the bug in SDL_mixer is fixed */
  90.   /* we don't want too many references to game objects here */
  91.   setMusicVolume(game->settings->musicVolume);
  92.   return;
  93. }
  94.  
  95. void stopSound() {
  96.   SOUND_IS_OK;
  97.   if( Mix_PlayingMusic() )
  98.     Mix_HaltMusic();
  99.   return;
  100. }
  101.  
  102. void soundIdle() {
  103.   /* sdl_mixer uses pthreads, so no work here */
  104.   return;
  105. }
  106.  
  107. int getFreeChannel() {
  108.   int i;
  109.   for(i = 0; i < MIX_CHANNELS; i++) {
  110.     if(!Mix_Playing(i)) {
  111.       return i;
  112.     }
  113.   }
  114.   return -1;
  115. }
  116.  
  117. void playGameFX(int fx, float volume) {
  118.   int channel;
  119.   SOUND_IS_OK;
  120.   if((channel = getFreeChannel()) >= 0) {
  121.        Mix_Volume(channel, volume * MIX_MAX_VOLUME);
  122.        Mix_PlayChannel(channel, game_fx[fx], 0);
  123.   }
  124. }
  125.  
  126. void playMenuFX(int fx) {
  127.   SOUND_IS_OK;
  128.   Mix_PlayChannel(-1, menu_fx[fx], 0);
  129. }
  130.  
  131. void setMusicVolume(float volume) {
  132.   SOUND_IS_OK;
  133.   if(volume > 1) volume = 1;
  134.   if(volume < 0) volume = 0;
  135.  
  136.   Mix_VolumeMusic((int)(volume * 128));
  137. }
  138.  
  139. void setFxVolume(float volume) {
  140.   SOUND_IS_OK;
  141.   if(volume > 1) volume = 1;
  142.   if(volume < 0) volume = 0;
  143.  
  144.   Mix_Volume(-1, (int)(volume * 128));
  145. }
  146.